package com.vulcan.model.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.vulcan.model.entity.Department;
import com.vulcan.model.entity.DepartmentDtl;
import com.vulcan.model.mapper.DepartmentDtlMapper;
import com.vulcan.model.mapper.DepartmentMapper;
import com.vulcan.util.StringToArrayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Allwayz
 * @since 2020-04-04
 */
@Slf4j
@Transactional
@RestController
@RequestMapping("/department-dtl")
public class DepartmentDtlController extends ApiController {
    @Autowired
    private DepartmentDtlMapper departmentDtlMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private StringToArrayUtil stringToArrayUtil;

    @GetMapping("{departmentName}")
    public R getDepartmentDtlByDepartment(@PathVariable String departmentName){
        Department department = departmentMapper.selectOne(new QueryWrapper<Department>().eq("department_name",departmentName));
        return success(departmentDtlMapper.selectList(new QueryWrapper<DepartmentDtl>().eq("department_id",department.getDepartmentId())));
    }

    /**
     * <p>
     *     模糊搜索症状
     * </p>
     * @param symptoms
     * @return
     */
    @GetMapping("symptoms/{symptoms}")
    public R getDepartmentDtlBySymptoms(@PathVariable String symptoms){
        List<String> stringList = stringToArrayUtil.StringToArray(symptoms," ");
        Set<DepartmentDtl> departmentDtlSet = new HashSet<>();
        for(String string:stringList){
            List<DepartmentDtl> departmentDtlList = departmentDtlMapper.selectList(new QueryWrapper<DepartmentDtl>().like("status",string));
            if(departmentDtlList.size() != 0){
                for(DepartmentDtl departmentDtl:departmentDtlList){
                    departmentDtlSet.add(departmentDtl);
                }
            }
        }
        Iterator<DepartmentDtl> departmentDtlIterator = departmentDtlSet.iterator();
        while (departmentDtlIterator.hasNext()){
            DepartmentDtl departmentDtl = departmentDtlIterator.next();
            log.info(departmentDtl.toString());
        }
        return success(departmentDtlSet);
    }
}
